package com.qyhl.webtv.module_live.utils.ItemViewDelegete;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.entity.live.TeleTextLiveBean;
import com.qyhl.webtv.commonlib.utils.view.ScoopButton;
import com.qyhl.webtv.commonlib.utils.view.expandableView.text.ExpandableTextView;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.utils.MediaListManager;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.lang.ref.WeakReference;
import java.util.List;


/**
 * Created by qyhl on 2017/8/14.
 */

public class RadioItemDelegete implements ItemViewDelegate<TeleTextLiveBean> {
    private final List<TeleTextLiveBean> liveBeen;
    private Context context;
    private String text;
    private ScoopButton myVoiceButton;

    public RadioItemDelegete(Context context,List<TeleTextLiveBean> liveBeen) {
        this.context = context;
        this.liveBeen=liveBeen;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.live_item_teletext_live_radio;
    }

    @Override
    public boolean isForViewType(TeleTextLiveBean item, int position) {
        if ("2".equalsIgnoreCase(item.getType())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void convert(ViewHolder holder, TeleTextLiveBean teleTextLiveBean, int position) {
        //设置时间轴
        TextView time = holder.getView(R.id.item_time);
        time.setText(DateUtils.teleTextDateFormat(teleTextLiveBean.getPublishtime()));//时间

        //设置作者
        TextView author = holder.getView(R.id.item_author);
        if (StringUtils.isEmpty(teleTextLiveBean.getAuthor())) {
            author.setVisibility(View.GONE);
        } else {
            author.setVisibility(View.VISIBLE);
            author.setText(StringUtils.encodePhoneNum(teleTextLiveBean.getAuthor()));
        }

        //设置简介
        ExpandableTextView content = holder.getView(R.id.item_content);
        if (StringUtils.isEmpty(teleTextLiveBean.getContent())) {
            content.setVisibility(View.GONE);
        } else {
            content.setVisibility(View.VISIBLE);
            content.setText(teleTextLiveBean.getContent(), position);
        }
        //设置日期
        TextView date = holder.getView(R.id.item_date);
        LinearLayout dateLayout = holder.getView(R.id.top_layout);
        if (position != 0) {
            if (liveBeen.size() > 0) {
                if (DateUtils.isSameDay(teleTextLiveBean.getPublishtime(),liveBeen.get(position - 1).getPublishtime())) {
                    dateLayout.setVisibility(View.GONE);
                } else {
                    dateLayout.setVisibility(View.VISIBLE);
                    date.setText(DateUtils.teleTextFormat(teleTextLiveBean.getPublishtime()));
                }
            } else {
                dateLayout.setVisibility(View.GONE);
            }
        } else {
            dateLayout.setVisibility(View.VISIBLE);
            date.setText(DateUtils.teleTextFormat(teleTextLiveBean.getPublishtime()));
        }
        ScoopButton scoopButton = holder.getView(R.id.radio_button);
        scoopButton.setContent(teleTextLiveBean.getAudioDuration());
        scoopButton.setTag(teleTextLiveBean);

        MediaListManager me = MediaListManager.getIntance();
        if (me.isPlay()) {
            me.release();
            //停止动画
            me.getTextView().endAnimation();
            //恢复时间
            me.getTextView().setContent(me.getContentBean().getAudioDuration());
        }

        scoopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GSYVideoManager.onPause();
                ScoopButton voiceButton = (ScoopButton) v;
                TeleTextLiveBean t = (TeleTextLiveBean) v.getTag();
                MediaListManager me = MediaListManager.getIntance();
                if (me.getTextView() != null) {
                    me.getTextView().endAnimation();
                    me.getTextView().setContent(me.getContentBean().getAudioDuration());
                }
                if (me.isPlay()) {
                    //停止播放语音
                    me.release();
                    //停止动画
                    me.getTextView().endAnimation();
                    //恢复时间
                    me.getTextView().setContent(me.getContentBean().getAudioDuration());

                    if (me.getContentBean().getId() == t.getId()) {
                        return;
                    }
                }

                //播放动画
                voiceButton.startAnimation();
                //添加
                me.setContentBean(t);
                me.setTextView(voiceButton);
                me.setOnProgressListener(new MediaListManager.OnProgressListener() {
                    @Override
                    public void Onprogress(ScoopButton te, String pro) {
                        myVoiceButton = te;
                        text = pro;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                mHandler.sendEmptyMessage(0);
                            }
                        }).start();
                    }
                });
                //播放音频
                me.playSound(t.getVideoUrl(), new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        MediaListManager me = MediaListManager.getIntance();
                        me.getTextView().endAnimation();
                        //恢复时间
                        me.getTextView().setContent(me.getContentBean().getAudioDuration());
                        Log.i("onCompletion", "播放音频");
                    }
                });

            }

        });
    }

    private final Handler mHandler = new MyHandler(this);

    private static class MyHandler extends Handler {
        private final WeakReference<RadioItemDelegete> mActivity;

        public MyHandler(RadioItemDelegete activity) {
            mActivity = new WeakReference<RadioItemDelegete>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            RadioItemDelegete radieo = mActivity.get();
            switch (msg.what) {
                case 0:
                    radieo.myVoiceButton.setContent(radieo.text);
                    break;
                default:
                    break;
            }
        }


    }


}
